package com.jordi.tetris.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jordi.tetris.model.Block;
import com.jordi.tetris.model.Board;
import com.jordi.tetris.model.Piece;
import com.jordi.tetris.model.pieces.IPiece;
import com.jordi.tetris.model.pieces.JPiece;
import com.jordi.tetris.model.pieces.LPiece;
import com.jordi.tetris.model.pieces.QPiece;
import com.jordi.tetris.model.pieces.SPiece;
import com.jordi.tetris.model.pieces.TPiece;
import com.jordi.tetris.model.pieces.ZPiece;

public class TetrisBoardView extends View {

	private Board board;

	public TetrisBoardView(Context context) {
		super(context);
	}

	public TetrisBoardView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public TetrisBoardView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TetrisBoardView(Context context, Board board) {
		super(context);
		this.board = board;
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	@Override
	protected void onDraw(Canvas canvas) {

		if (board != null) {
			int rows = board.getRows();
			int columns = board.getColumns();

			int sideH = getHeight() / rows;
			int sideW = getWidth() / columns;

			for (int i = 0; i < rows; i++) {
				for (int j = 0; j < columns; j++) {
					Block b = board.getBlock(i, j);
					if (!b.isEmpty()) {
						ShapeDrawable blockDr = new ShapeDrawable(
								new RectShape());
						blockDr.setBounds(j * sideW, i * sideH, (j * sideW)
								+ sideW, (i * sideH) + sideH);

						blockDr.getPaint().setColor(getColorForBlock(b));
						blockDr.draw(canvas);
					}
				}
			}

			Piece currentPiece = board.getCurrePiece();

			for (int i = 0; i < currentPiece.getHeight(); i++) {
				for (int j = 0; j < currentPiece.getWidth(); j++) {
					Block b = currentPiece.getBlock(i, j);
					if (!b.isEmpty()) {
						ShapeDrawable blockDr = new ShapeDrawable(
								new RectShape());
						blockDr.setBounds((currentPiece.getX() + j) * sideW,
								(currentPiece.getY() + i) * sideH,
								((currentPiece.getX() + j) * sideW) + sideW,
								((currentPiece.getY() + i) * sideH) + sideH);

						blockDr.getPaint().setColor(getColorForBlock(b));
						blockDr.draw(canvas);
					}
				}
			}

			super.onDraw(canvas);
		}
	}

	private int getColorForBlock(Block b) {
		Piece p = b.getPiece();

		if (p instanceof LPiece) {
			return Color.BLUE;
		} else if (p instanceof IPiece) {
			return Color.GREEN;
		} else if (p instanceof QPiece) {
			return Color.RED;
		} else if (p instanceof TPiece) {
			return Color.YELLOW;
		} else if (p instanceof ZPiece) {
			return Color.GRAY;
		} else if (p instanceof JPiece) {
			return Color.CYAN;
		} else if (p instanceof SPiece) {
			return Color.MAGENTA;
		} else {
			return Color.TRANSPARENT;
		}
	}
}
